<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" validateTrigger="bind">
			<uni-forms-item name="s_id" label="课程ID">
				<uni-easyinput v-model="formData.s_id"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="name" label="课程昵称">
				<uni-easyinput v-model="formData.name"></uni-easyinput>
			</uni-forms-item>
			<!--      <uni-forms-item name="type_subject" label="科目类型">
        <uni-easyinput v-model="formData.type_subject"></uni-easyinput>
      </uni-forms-item> -->

			<uni-forms-item name="type_subject" label="科目类型">
				<uni-data-select v-model="formData.type_subject" required collection="type-subject"
					field="s_id as value, name as text">
				</uni-data-select>
			</uni-forms-item>


			<uni-forms-item name="type_drive_id" label="驾驶证类型">
				<uni-data-select v-model="formData.type_drive_id" ref="drive_udb" collection="type-drive,type-cert"
					field="s_id as value, name as text,_id,type_cert_id">
				</uni-data-select>
			</uni-forms-item>


			<uni-forms-item name="video_link" label="视频链接">
				<!-- <uni-data-checkbox :multiple="true" v-model="formData.video_link"></uni-data-checkbox> -->
				<uni-easyinput v-model="formData.video_link"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="video_name" label="视频昵称">
				<!-- <uni-data-checkbox :multiple="true" v-model="formData.video_name"></uni-data-checkbox> -->
				<uni-easyinput v-model="formData.video_name"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="test_paper_ids" label="试卷">


				<a-button @click="importTest()">
					导入
				</a-button>
				<a-row type="flex">
					<a-col style="background-color: cornflowerblue;padding:12rpx;margin:12rpx;color:#fff" v-for="(item,index) in test_paper_ids_loc" :key="item">
						{{
							item
						}}
					</a-col>
				</a-row>
				
				
			</uni-forms-item>
			<uni-forms-item name="sort" label="排序">
				<uni-easyinput type="number" v-model="formData.sort"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="status" label="启用状态">
				<uni-data-checkbox v-model="formData.status" :localdata="formOptions.status_localdata">
				</uni-data-checkbox>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '@/js_sdk/validator/subject-page.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'subject-page';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}

	export default {
		data() {
			let formData = {
				"s_id": "",
				"name": "",
				"type_subject": "",
				"type_drive_id": [],
				"video_link": '',
				"video_name": '',
				"test_paper_ids": [],
				"sort": 0,
				"status": 0
			}

			return {

				test_paper_ids_loc: [],
				t_sort_loc: [],
				formData,
				formOptions: {
					"status_localdata": [{
							"text": "正常",
							"value": 0
						},
						{
							"text": "禁用",
							"value": 1
						}
					]
				},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			importTest() {
				const self = this
				uni.chooseFile({
					async success(res) {
						const url = res.tempFilePaths[0];
						const data = await (await fetch(url, {
							mode: 'no-cors'
						})).arrayBuffer();
						/* data is an ArrayBuffer */
						const workbook = self.$XLSX.read(data);
						const xlsxData = self.$XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[
							0]])

						for (let rowData of xlsxData) {
							self.test_paper_ids_loc.push(rowData['试卷'])
							self.t_sort_loc.push(rowData['试卷排序'])
						}

					}
				})

			},
			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				value.sid = Number(value.s_id)
				return db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			}
		}
	}
</script>
